package com.zemb.flxwear.Utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.util.Log;


import com.zemb.flxwear.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class VoicePlayUtils {
//    //单例模式
    private int bloodPressureDeviceSoundItem = R.raw.bp_device;
    private int bloodOxygenDeviceSoundItem = R.raw.bo_device;
    private int electrocardiogramDeviceSoundItem = R.raw.egc_device;
    private int fourCategoryDeviceSoundItem = R.raw.bf_device;
    private int connectSuccessSoundItem = R.raw.connect_success;
    private int connectFailedSoundItem = R.raw.connect_failed;
    private int connectStartSoundItem = R.raw.connect_start;
    private int disconnectSoundItem = R.raw.disconnect;
    private int measureSuccessSoundItem = R.raw.measure_success;
    private int measureFailedSoundItem = R.raw.measure_failed;
    private int measureStartSoundItem = R.raw.measure_start;
    private int welcomeSoundItem = R.raw.welcome;

    private int thus_blood_ketone = R.raw.thus_blood_kthon_paper;

    private int thus_blood_sugar = R.raw.thus_blood_sugar_paper;

    private int thus_uric_acid = R.raw.thus_ucal_acid_paper;

    private int thus_cholesterol = R.raw.thus_cho_paper;

    private int paper_right = R.raw.ture_paper_wait_measure;

    private int paper_worse = R.raw.worse_paper_pls_try_agan;
    private Context context;
    private static VoicePlayUtils instance;
    private MediaPlayer mediaPlayer;
    boolean isReleased =false;
    private VoicePlayUtils(){

    }
    public static VoicePlayUtils getInstance(){
        if(instance == null){
            synchronized (VoicePlayUtils.class){
                if(instance == null){
                    instance = new VoicePlayUtils();
                }
            }
        }
        return instance;
    }
//    //初始化
    public void onCreate(Context context){
        this.context = context;
        mediaPlayer = createMediaPlayer( R.raw.welcome);
        mediaPlayer.setLooping(true);

    }




    private void Play(List<Integer> sounds){
        LogUtils.PrintLog("playSound");
        if(mediaPlayer!=null&&!isReleased && mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }
        mediaPlayer =createMediaPlayer(sounds.get(0));
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                sounds.remove(0);
                if(sounds.size()>0){
                    Play(sounds);
                }else {
                   release();
                }
            }
        });
        mediaPlayer.start();
    }

    public void PlayWelcome(){
        List<Integer> sounds = new ArrayList<>();
        sounds.add(welcomeSoundItem);
        Play(sounds);
    }
    //开始连接
    public void PlayConnectStart(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        sounds.add(connectStartSoundItem);
        Play(sounds);
    }
    //连接成功
    public void PlayConnectSuccess(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        sounds.add(connectSuccessSoundItem);
        if(category == DeviceUtils.Category.BLOOD_KETONE||category == DeviceUtils.Category.BLOOD_SUGAR||category == DeviceUtils.Category.URIC_ACID||category == DeviceUtils.Category.CHOLESTEROL){
            sounds.addAll(getThusPaper(category));
        }
        Play(sounds);
    }
    //连接失败
    public void PlayConnectFailed(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        sounds.add(connectFailedSoundItem);
        Play(sounds);
    }
    //断开连接
    public void PlayDisconnect(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        sounds.add(disconnectSoundItem);
        Play(sounds);
    }
    //开始测量
    public void PlayMeasureStart(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        sounds.add(measureStartSoundItem);
        //如果是血液四项

        Play(sounds);
    }
    //测量成功
    public void PlayMeasureSuccess(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        sounds.add(measureSuccessSoundItem);
        Play(sounds);
    }
    //测量失败
    public void PlayMeasureFailed(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        sounds.add(measureFailedSoundItem);
        Play(sounds);
    }


    private int getDevice(DeviceUtils.Category category){
        switch (category) {
            case BLOOD_PRESSURE:
                return bloodPressureDeviceSoundItem;

            case BLOOD_OXYGEN:

                return bloodOxygenDeviceSoundItem;
            case ECG:

                return electrocardiogramDeviceSoundItem;
            case BLOOD_KETONE:
            case CHOLESTEROL:
            case BLOOD_SUGAR:
            case URIC_ACID:
                return fourCategoryDeviceSoundItem;
            default:
                return 0;
        }
    }
    private void release(){
        mediaPlayer.release();
        isReleased = true;
    }
    private MediaPlayer createMediaPlayer(int sound){
        mediaPlayer = MediaPlayer.create(context,sound);
        isReleased =false;
        return mediaPlayer;
    }

    public List<Integer> getThusPaper(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        switch (category){
            case BLOOD_KETONE:
                sounds.add(thus_blood_ketone);
                break;
            case BLOOD_SUGAR:
                sounds.add(thus_blood_sugar);
                break;
            case URIC_ACID:
                sounds.add(thus_uric_acid);
                break;
            case CHOLESTEROL:
                sounds.add(thus_cholesterol);
                break;
        }
        return sounds;
    }

    public void PlayThusPaper(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        if(getDevice(category)!=0){
            sounds.add(getDevice(category));
        }
        switch (category){
            case BLOOD_KETONE:
                sounds.add(thus_blood_ketone);
                break;
            case BLOOD_SUGAR:
                sounds.add(thus_blood_sugar);
                break;
            case URIC_ACID:
                sounds.add(thus_uric_acid);
                break;
            case CHOLESTEROL:
                sounds.add(thus_cholesterol);
                break;
        }
        Play(sounds);
    }

    public void playPaperWorse(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        sounds.add(paper_worse);
        //如果是血液四项
        sounds.addAll(getThusPaper(category));
        Play(sounds);
    }

    public void playPaperRight(DeviceUtils.Category category){
        List<Integer> sounds = new ArrayList<>();
        sounds.add(paper_right);
        Play(sounds);
    }
}
